
#include<vector>

#include <unistd.h>
#include <errno.h>
#include <sys/iomsg.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "../m_connection_tests/komunikacja1.2.h"

using namespace std;

char * Message::verboseType(int _type)
{
		switch ( _type)
		{
				
			case  GET_AIRPORTS_EV_DB :
				return "GET_AIRPORTS_EV_DB";
				
			case  RES_GET_AIRPORTS_EV_DB :
				return "RES_GET_AIRPORTS_EV_DB";
			case  GET_AIRPORTS_PL_DB :
				return "GET_AIRPORTS_PL_DB";
			case  GET_PLANES_EV_DB :
				return "GET_PLANES_EV_DB";

			case  RES_GET_PLANES_EV_DB :
				return "RES_GET_PLANES_EV_DB";
			case GET_EVENTS_EV_DB :
				return "GET_EVENTS_EV_DB";
			case  RES_GET_EVENTS_EV_DB :
				return "RES_GET_EVENTS_EV_DB";

			case  GET_AIRPORT_AI_DB :
				return "GET_AIRPORT_AI_DB";
			case RES_GET_AIRPORT_AI_DB :
				return "RES_GET_AIRPORT_AI_DB";
			case  GET_PLANE_AI_DB :
				return "GET_PLANE_AI_DB";

			case  RES_GET_PLANE_AI_DB :
				return "RES_GET_PLANE_AI_DB";
			case GET_AIRPORT_PL_DB :
				return "GET_AIRPORT_PL_DB";
			case  GET_AIRPLANE_PL_DB :
				return "GET_AIRPLANE_PL_DB";
				
			case  FAILURE_EV_PL :
				return "FAILURE_EV_PL";
			case FAILURE_EV_PO :
				return "FAILURE_EV_PO";
			case  FAILURE_OVER_EV_PL :
				return "FAILURE_OVER_EV_PL";

			case  FAILURE_OVER_EV_PO :
				return "FAILURE_OVER_EV_PO";
			case ACTIVATE_AIRPORT :
				return "ACTIVATE_AIRPORT";
			case  DISACTIVATE_AIRPORT :
				return "DISACTIVATE_AIRPORT";
				
			case ZAKAZ_LADOWANIA :
				return "ZAKAZ_LADOWANIA";
		
				
			case  ZAKAZ_STARTU :
				return "ZAKAZ_STARTU";

			case BD_MOD :
				return "BD_MOD";
			case WIEZA_MOD	 :
				return "WIEZA_MOD";
			case ZDARZENIA_MOD	 :
				return "ZDARZENIA_MOD";

			case MSG_ZGLOSZENIE_GOTOWOSCI_DO_LADOWANIA :
				return "MSG_ZGLOSZENIE_GOTOWOSCI_DO_LADOWANIA";
			case MSG_ZGLOSZENIE_GOTOWOSCI_DO_STARTU :
				return "MSG_ZGLOSZENIE_GOTOWOSCI_DO_STARTU";
			case MSG_ZGLOSZENIE_AWARII	 :
				return "MSG_ZGLOSZENIE_AWARII";

			case MSG_STAN_PALIWA :
				return "MSG_STAN_PALIWA";
			case MSG_AKTUALNA_POZYCJA :
				return "MSG_AKTUALNA_POZYCJA";
			case MSG_NOWA_MASZYNA_W_STREFIE	 :
				return "MSG_NOWA_MASZYNA_W_STREFIE";
				
			case MSG_ZAPODAJ_NAJBLIZSZE_LOTNISKO :
				return "MSG_ZAPODAJ_NAJBLIZSZE_LOTNISKO";
			case MSG_SIE_ZESRRRALES :
				return "MSG_SIE_ZESRRRALES";
			case MSG_KONIEC_AWARII		 :
				return "MSG_KONIEC_AWARII";

			case MSG_ZEZWOLENIE_NA_START :
				return "MSG_ZEZWOLENIE_NA_START";
			case MSG_ZMIEN_KURS :
				return "MSG_ZMIEN_KURS";
			case MSG_POCZEKAJ_Z_LADOWANIEM		 :
				return "MSG_POCZEKAJ_Z_LADOWANIEM";
	
			case MSG_MOZESZ_LADOWAC :
				return "MSG_MOZESZ_LADOWAC";
			case MSG_OPUSCILES_STREFE_WPLYWOW:
				return "MSG_OPUSCILES_STREFE_WPLYWOW";
			case MSG_ZAPODAJ_WSPOLRZEDNE_LOTNISKA	 :
				return "MSG_ZAPODAJ_WSPOLRZEDNE_LOTNISKA";

			case MSG_LAP_WSPOLRZEDNE_LOTNISKA :
				return "MSG_LAP_WSPOLRZEDNE_LOTNISKA";
			case MSG_PODAJ_LICZBE_LOTNISK :
				return "MSG_PODAJ_LICZBE_LOTNISK";
			case MSG_PRZESLIJ_LOTNISKA		 :
				return "MSG_PRZESLIJ_LOTNISKA";

			case MSG_LAP_LICZBE_LOTNISK:
				return "MSG_LAP_LICZBE_LOTNISK";
			case MSG_KONCZE_SMIGAC:
				return "MSG_KONCZE_SMIGAC";
			case MSG_GON_SIE		 :
				return "MSG_GON_SIE	";

			case MSG_ZAPODAJ_DANE_TECHNICZNE :
				return "MSG_ZAPODAJ_DANE_TECHNICZNE";
			case  MSG_SIE_ODESRALES :
				return " MSG_SIE_ODESRALES";
			case MSG_RES_ZAPODAJ_DANE_TECHNICZNE		 :
				return "MSG_RES_ZAPODAJ_DANE_TECHNICZNE";

			case MSG_POJAWIAM_SIE_NA_LOTNISKU :
				return "MSG_POJAWIAM_SIE_NA_LOTNISKU";
				
			default:
				return "Unknown message type";
				
		}
}

Message::Message()
{
	data.type = -1; 

	for( int i = 0; i < CONN_NAME_LENGTH; i++) {
		data.connSenderName[i] = NULL;
		data.connReceiverName[i] = NULL;
		data.argStr1[i] = NULL;
		data.argStr2[i] = NULL;
	}
		
		
	data.msgLength = -1;
	data.nr = -1;
	
	
	data.argInt1 = -1;
	data.argInt2 = -1;
	data.argInt3 = -1;
	data.argInt4 = -1;
	
	data.argBool1 = true;

	data.argFloat1 = -1;
	data.argFloat2 = -1;	
	data.argFloat3 = -1;	
	
	
}




	
void Message::dumpMessage( void)
{


		cout <<"\n\n" <<"Message.DumpMessage()"
     			<<"\n" <<"type : " << verboseType(getType())
     			<<"\n" <<"type : " << getType()
     			<<"\n" <<"msgLength : " << getMessageLength()
     			<<"\n" <<"nr : " << getNr()
				<<"\nargInt1 : " << getIntArg1()
				<<"\nargInt2 : " << getIntArg2()
				<<"\nargInt3 : " << getIntArg3()
				<<"\nargInt4 : " << getIntArg4()
				<<"\nargBool1 : " << getBoolArg1()
				<<"\nargFloat1 : " << getFloatArg1()
				<<"\nargFloat2 : " << getFloatArg2()
				<<"\nargString1 : " << getStringArg1()
				<<"\nConnSenderName : "<<getConnSenderName()
				<<"\nConnReceiverName : "<<getConnReceiverName()
				<<endl;
}

/**
			gety i sety
**/



void Message::setType( int _type)
{
     data.type = _type;
}

int Message::getType( void)
{
     return data.type;
}

char * Message::getConnSenderName(void)
{
	return data.connSenderName;
}

void Message::setConnSenderName(char* arg)
{
	//cout << "otrzymalem :" << arg <<endl;
	for(int i = 0 ; i < CONN_NAME_LENGTH; i++)
	{
		if( *(arg+i) != NULL)
			data.connSenderName[i] = *(arg+i);
		else
			data.connSenderName[i] = NULL;
	}
	//cout << "setConnName. Nowa nazwa : " << data.connName <<endl;
	return;
}

char * Message::getConnReceiverName(void)
{
	return data.connReceiverName;
}

void Message::setConnReceiverName(char* arg)
{
	//cout << "otrzymalem :" << arg <<endl;
	for(int i = 0 ; i < CONN_NAME_LENGTH; i++)
	{
		if( *(arg+i) != NULL)
			data.connReceiverName[i] = *(arg+i);
		else
			data.connReceiverName[i] = NULL;
	}
	//cout << "setConnName. Nowa nazwa : " << data.connReceiverName <<endl;
	return;
}


void Message::setMessageLength( int arg)
{
	data.msgLength = arg;
}

int Message::getMessageLength(void)
{
	return data.msgLength;
}
   
void Message::setNr( int arg)
{
	data.nr = arg;
}

int Message::getNr(void)
{
	return data.nr;
}


int Message::getIntArg1(void)
{
	return data.argInt1;
}

void Message::setIntArg1(int arg)
{
	data.argInt1 = arg;
}

int Message::getIntArg2(void)
{
	return data.argInt2;
}

void Message::setIntArg2(int arg)
{
	data.argInt2 = arg;
}

int Message::getIntArg3(void)
{
	return data.argInt3;
}

void Message::setIntArg3(int arg)
{
	data.argInt3 = arg;
}

int Message::getIntArg4(void)
{
	return data.argInt4;
}

void Message::setIntArg4(int arg)
{
	data.argInt4 = arg;
}

bool Message::getBoolArg1(void)
{
	return data.argBool1;
}

void Message::setBoolArg1(bool arg)
{
	data.argBool1 = arg;
}
	
	
float Message::getFloatArg1(void)
{
	return data.argFloat1;
}

void Message::setFloatArg1( float arg)
{
	data.argFloat1 = arg;
}

float Message::getFloatArg2(void)
{
	return data.argFloat2;
}

void Message::setFloatArg2( float arg)
{
	data.argFloat2 = arg;
}

float Message::getFloatArg3(void)
{
	return data.argFloat3;
}

void Message::setFloatArg3( float arg)
{
	data.argFloat3 = arg;
}


char * Message::getStringArg1(void)
{
	return data.argStr1;
}

void Message::setStringArg1( char * arg)
{
	
	for(int i = 0 ; i < CONN_NAME_LENGTH; i++)
	{
		if( *(arg+i) != NULL)
			data.argStr1[i] = *(arg+i);
		else
			data.argStr1[i] = NULL;
	}

	return;
}



char * Message::getStringArg2(void)
{
	return data.argStr2;
}

void Message::setStringArg2( char * arg)
{
	
	for(int i = 0 ; i < CONN_NAME_LENGTH; i++)
	{
		if( *(arg+i) != NULL)
			data.argStr2[i] = *(arg+i);
		else
			data.argStr2[i] = NULL;
	}

	return;
}



void Message::copyMessage( Message * ptr)
{
	ptr->data.type = data.type;

	strncpy( ptr->data.connSenderName,  data.connSenderName,  CONN_NAME_LENGTH);
	strncpy( ptr->data.connReceiverName, data.connReceiverName,  CONN_NAME_LENGTH);
	strncpy( ptr->data.argStr1, data.argStr1,   CONN_NAME_LENGTH);
	strncpy( ptr->data.argStr2, data.argStr2,   CONN_NAME_LENGTH);
	ptr->data.nr = data.nr;						// nr elementu w wektorze	
	ptr->data.msgLength = data.msgLength;			// dlugosc przesylanego wektora
	
	ptr->data.argInt1 = data.argInt1;;
	ptr->data.argInt2 = data.argInt2;
	ptr->data.argInt3 = data.argInt3;
	ptr->data.argInt4 = data.argInt4;
	
	ptr->data.argBool1 = data.argBool1;
	
	ptr->data.argFloat1 = data.argFloat1;
	ptr->data.argFloat2 = data.argFloat2;
	ptr->data.argFloat3 = data.argFloat3;;
	
}

char * Connector::getConnName( void)
{
	return connName;
}





